package com.liuyfly.leetcode.questions;

import lombok.extern.slf4j.Slf4j;

/**
 * @author liuyangfang at 2022-08-22 21:17
 */
@Slf4j
public class Question0007 {
    /**
     * 给你一个32位的有符号整数x，返回将x中的数字部分反转后的结果。如果反转后整数超过32位的有符号整数的范围[−2^31,2^31−1]，就返回0。<br/>
     * <br/>
     * 链接：https://leetcode.cn/problems/reverse-integer
     */
    public static int reverse(int x) {
        String str = String.valueOf(x);
        char[] array = str.toCharArray();
        StringBuilder builder = new StringBuilder();
        int loopEnd = 0;
        if (x < 0) {
            loopEnd = 1;
        }
        for (int i = array.length - 1; i >= loopEnd; i--) {
            builder.append(array[i]);
        }
        String str2 = builder.toString();
        long valueLong = Long.parseLong(str2);
        int valueInt = (int) valueLong;
        if (valueLong != (long) valueInt) {
            return 0;
        }
        if (x < 0) {
            valueInt = -valueInt;
        }
        return valueInt;
    }

    public static void main(String[] args) {
        int a = -147483640;
        int result = reverse(a);
        log.info("result:[{}]", result);
    }
}
